package com.dbpms.pj.sys.service.impl;

import com.dbpms.pj.common.exception.ServiceException;
import com.dbpms.pj.common.pojo.CheckBox;
import com.dbpms.pj.common.pojo.PageObject;
import com.dbpms.pj.sys.dao.SysRoleDao;
import com.dbpms.pj.sys.dao.SysRoleMenuDao;
import com.dbpms.pj.sys.pojo.SysRole;
import com.dbpms.pj.sys.pojo.SysRoleMenu;
import com.dbpms.pj.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: Song Yu
 * @Date: 2020/10/20 9:57
 */

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    SysRoleDao sysRoleDao;

    @Autowired
    SysRoleMenuDao sysRoleMenuDao;

    @Override
    public PageObject<SysRole> findPageObjects(String username, Integer pageCurrent) {
        if (pageCurrent <= 0 || pageCurrent == null)
            throw new ServiceException("当前页码值不正确");
        int rowCount = sysRoleDao.getRowCount(username);
        if (rowCount == 0)
            throw new ServiceException("没有找到对应记录");
        int pageSize = 5;
        int startIndex = (pageCurrent - 1) * pageSize;
        List<SysRole> records = sysRoleDao.findPageObjects(username, startIndex, pageSize);
        return new PageObject<>(pageCurrent, pageSize, rowCount, records);
    }

    @Override
    public int deleteObject(Integer id) {
        if(id==null)
            throw new ServiceException("请先选择一条数据");
        int rows;
        try{
            rows=sysRoleDao.deleteObject(id);
        }catch(Throwable e){
            e.printStackTrace();
            //发出报警信息(例如给运维人员发短信)
            throw new ServiceException("系统故障，正在恢复中...");
        }
        //4.对结果进行验证
        if(rows==0)
            throw new ServiceException("记录可能已经不存在");
        sysRoleMenuDao.deleteObjectsByRoleId(id);
        //5.返回结果
        return rows;
    }

    @Override
    public int saveObject(SysRole entity, Integer[] menuIds) {
        //1.参数有效性校验
        if(entity==null)
            throw new ServiceException("保存对象不能为空");
        if(StringUtils.isEmpty(entity.getName()))
            throw new ServiceException("角色名不允许为空");
        if(menuIds==null||menuIds.length==0)
            throw new ServiceException("必须为角色分配权限");
        //2.保存角色自身信息
        int rows=sysRoleDao.insertObject(entity);
        //3.保存角色菜单关系数据
        sysRoleMenuDao.insertObjects(entity.getId(), menuIds);
        //4.返回业务结果
        return rows;
    }

    @Override
    public SysRoleMenu findById(Integer id) {
        SysRoleMenu sysRoleMenu = sysRoleDao.findById(id);
        return sysRoleMenu;
    }

    @Override
    public int updateObject(SysRole entity,Integer[] menuIds) {
        //1.参数有效性校验
        if(entity==null)
            throw new ServiceException("保存对象不能为空");
        if(StringUtils.isEmpty(entity.getName()))
            throw new ServiceException("角色名不允许为空");
        if(menuIds==null||menuIds.length==0)
            throw new ServiceException("必须为角色分配权限");
        //2.更新角色自身信息
        int rows=sysRoleDao.updateObject(entity);
        if(rows == 0)
            throw new ServiceException("对象可能已经不存在");
        //3.更新角色菜单关系数据
        sysRoleMenuDao.deleteObjectsByRoleId(entity.getId());
        sysRoleMenuDao.insertObjects(entity.getId(), menuIds);
        //4.返回业务结果
        return rows;
    }

    @Override
    public List<CheckBox> findObjects() {
        return sysRoleDao.findObjcets();
    }
}
